package cn.wen.java.awt.image;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.AffineTransformOp;

import java.io.IOException;
import java.io.File;

import javax.imageio.ImageIO;

/**
 * Title：平面图像工具类
 * <p>
 * Description：
 * <p>
 * Copyright：Copyright (c) 2010-2012
 * 
 * @author <a href="mailto:ct_wen@qq.com">ctwen</a>, 温承瑭
 * @version Revision：1.0 Date：2010/7/17 Description：初完成。
 *          <p>
 */
@Deprecated
public class HelperOfImage {

    /**
     * 保存图片为JPG格式。
     * 
     * @param file 要读取的文件对象
     * @param saveFile 要保存的文件对象
     * @param width 宽度
     * @param hight 高宽
     * @return 是否成功。
     */
    public static boolean processingImageSizeAsJpg(File file, File saveFile, int width, int hight) {
	BufferedImage srcImage;
	try {
	    srcImage = ImageIO.read(file);
	    if (width > 0 || hight > 0) srcImage = resize(srcImage, width, hight);
	    ImageIO.write(srcImage, "JPEG", saveFile);
	    return true;
	}
	catch (Exception e) {
	    return false;
	}
    }

    /**
     * 图片水印
     * 
     * @param pressImg 水印图片
     * @param targetImg 目标图片
     * @param x 修正值 默认在中间
     * @param y 修正值 默认在中间
     * @param alpha 透明度
     */
    public final static void pressImage(String pressImg, String targetImg, int x, int y, float alpha) {
	try {
	    File img = new File(targetImg);
	    Image src = ImageIO.read(img);
	    int wideth = src.getWidth(null);
	    int height = src.getHeight(null);
	    BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
	    Graphics2D g = image.createGraphics();
	    g.drawImage(src, 0, 0, wideth, height, null);
	    // 水印文件
	    Image src_biao = ImageIO.read(new File(pressImg));
	    int wideth_biao = src_biao.getWidth(null);
	    int height_biao = src_biao.getHeight(null);
	    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
	    g.drawImage(src_biao, (wideth - wideth_biao) / 2, (height - height_biao) / 2, wideth_biao, height_biao, null);
	    // 水印文件结束
	    g.dispose();
	    ImageIO.write((BufferedImage) image, "jpg", img);
	}
	catch (Exception e) {
	    e.printStackTrace();
	}
    }

    /**
     * 缩放
     * 
     * @param filePath 图片路径
     * @param height 高度
     * @param width 宽度
     * @param bb 比例不对时是否需要补白
     */
    public static void resize(File img, int height, int width, boolean bb) {
	try {
	    double ratio = 0.0; // 缩放比例
	    BufferedImage bi = ImageIO.read(img);
	    Image itemp = bi.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
	    // 计算比例
	    if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
		if (bi.getHeight() > bi.getWidth()) {
		    ratio = (new Integer(height)).doubleValue() / bi.getHeight();
		}
		else {
		    ratio = (new Integer(width)).doubleValue() / bi.getWidth();
		}
		AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
		itemp = op.filter(bi, null);
	    }
	    if (bb) {
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();
		g.setColor(Color.white);
		g.fillRect(0, 0, width, height);
		if (width == itemp.getWidth(null)) {
		    g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null),
			    Color.white, null);
		}
		else {
		    g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null),
			    Color.white, null);
		}
		g.dispose();
		itemp = image;
	    }
	    ImageIO.write((BufferedImage) itemp, "jpg", img);
	}
	catch (IOException e) {
	    e.printStackTrace();
	}
    }

    /**
     * 处理图片的缩放。
     * 
     * @param source 图像源
     * @param targetW 图像宽
     * @param targetH 图像高
     * @return BufferedImage
     * @throws RuntimeException
     */
    public static BufferedImage resize(BufferedImage source, int targetW, int targetH) throws RuntimeException {

	// targetW，targetH分别表示目标长和宽
	int type = source.getType();
	BufferedImage target = null;
	double sx = (double) targetW / source.getWidth();
	double sy = (double) targetH / source.getHeight();

	if (type != BufferedImage.TYPE_CUSTOM) target = new BufferedImage(targetW, targetH, type);
	else {
	    ColorModel cm = source.getColorModel();
	    WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
	    boolean alphaPremultiplied = cm.isAlphaPremultiplied();
	    target = new BufferedImage(cm, raster, alphaPremultiplied, null);
	}

	Graphics2D g = target.createGraphics();
	// smoother than exlax:
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

	g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
	g.dispose();

	return target;
    }

}
